package com.example.testdemo.douyin;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 放抖音录制按钮
 * Created by zhangwenxiang on 2018/7/24.
 * 暂不提供外部调节速度方法
 */

public class RecordVideoView extends View{
    private static final String TAG = "RecordVideoView";
    private Paint outPaint;//外圆画笔
    private Paint inPaint;//内圆画笔

    private int recordState;//录制状态 0.未录制 1.录制种 2.录制完成
    private float mXCenter;//x中心点 宽度配置需固定 //  private float mYCenter;//y中心点
    private float mRadius;//半径
    private float mStroke = 20;//圆环粗细
    private float sLeft, sTop, sRight, sBottom;//方形 or 圆位置
    //圆动画
    private float mAnimRadius;//半径
    private float animStroke = 30;//动画圆环 默认大小
    private float minStorke = 20;
    private float maxSotrke = 40;
    private boolean isAnimSub = true;//是否动画缩小

    public RecordVideoView(Context context) {
        super(context);
        initPaint();
    }
    public RecordVideoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }
    private void initPaint(){
        //外圆（动画）
        outPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        outPaint.setColor(Color.parseColor("#FF4A74"));
        outPaint.setStyle(Paint.Style.STROKE);
        outPaint.setAlpha(90);
        //内圆（方形）
        inPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        inPaint.setColor(Color.parseColor("#FF4A74"));
    }
    //临时变量数据 用于view过渡
    private float xNum;
    private float xNum2;
    private float rxNum;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mXCenter = getWidth() / 2;//x中心点
        if(recordState == 0){//未录制 (静止)
            //画外环
            mRadius = mXCenter - mStroke * 2.5f;//外圆半径
            outPaint.setStrokeWidth(20);//描边
            canvas.drawCircle(mXCenter, mXCenter, mRadius , outPaint);
            //画内圆
            canvas.drawCircle(mXCenter, mXCenter, mRadius - 15, inPaint);
        }else if(recordState == 1){//录制中 （启动动画）
            //画内方形
            float minSqRate = mXCenter / 3;
            float defSqRate = (mRadius -15) - xNum ;//mXCenter / 2 - xNum;
            if(defSqRate > minSqRate){
                xNum += 2;
            }
            sLeft = sTop =  mXCenter - (defSqRate);
            sRight = sBottom = mXCenter + (defSqRate);
            RectF rect = new RectF(sLeft, sTop, sRight, sBottom);
            //正圆切换
            float maxRx = (sRight - sLeft)/2;
            float currRx = maxRx - rxNum > 10 ? maxRx - rxNum : 10;
            if(currRx > 10){
                rxNum+=2;
            }
            canvas.drawRoundRect(rect, currRx, currRx, inPaint);

            //画动画外圆
            mAnimRadius = mXCenter - 10;//动画圆半径
            //计算边框
            if(animStroke > minStorke && isAnimSub){
                animStroke -= 0.5;
                if(animStroke == minStorke)isAnimSub = false;
            }else if(animStroke < maxSotrke && !isAnimSub){
                animStroke += 0.5;
                if(animStroke == maxSotrke)isAnimSub = true;
            }
            Log.i(TAG, "STROKE="+animStroke+"  CURRRX="+ currRx);
            //计算半径
            mAnimRadius = mAnimRadius - animStroke/2;
            //描边
            outPaint.setStrokeWidth(animStroke);
            canvas.drawCircle(mXCenter, mXCenter, mAnimRadius , outPaint);

            postInvalidate();
        }else {//录制完成
            //方形 to 圆
            float maxSqRate = (mRadius - 15) ;//mXCenter / 2 - xNum;
            float defSqRate = mXCenter / 3 + xNum;
            if(defSqRate < maxSqRate){
                xNum += 2;
            }
            sLeft = sTop =  mXCenter - (defSqRate);
            sRight = sBottom = mXCenter + (defSqRate);
            RectF rect = new RectF(sLeft, sTop, sRight, sBottom);
            //正圆切换
            float maxRx = (sRight - sLeft)/2;
            float currRx = 10 + rxNum <= maxRx  ? 10 + rxNum : maxRx;
            if(currRx < maxRx){
                rxNum += 4;
            }
            canvas.drawRoundRect(rect, currRx, currRx, inPaint);
            //画外圆
            float outRadius = mXCenter - mStroke * 2.5f;//外圆半径
            float nowOutRadius = mAnimRadius + xNum2;
            if(nowOutRadius > outRadius){
                xNum2--;
            }
            outPaint.setStrokeWidth(20);//描边
            canvas.drawCircle(mXCenter, mXCenter, nowOutRadius , outPaint);
            //刷新绘制
            if(currRx < maxRx || nowOutRadius > outRadius){
                postInvalidate();
            }
        }
    }


    /**
     * 开始录制
     */
    public void onStart(){
        if(recordState == 1){
            return;
        }
        clearOldData();
        recordState = 1;
        postInvalidate();
    }

    /**
     * 停止录制
     */
    public void onStop(){
        if(recordState != 1){
            return;
        }
        clearOldData();
        recordState = 0;
        postInvalidate();
    }


    public void onSuccess(){
        if(recordState == 2){
            return;
        }
        clearOldData();
        recordState = 2;
        postInvalidate();
    }

    /**
     * 清空临时旧数据
     */
    private void clearOldData(){
        xNum = xNum2 = rxNum =0;

    }
}
